package gui.form;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import Model.Owner;
import dao.DaoOwner;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

import javax.swing.LayoutStyle.ComponentPlacement;

public class OwnerPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	private JTextField name;
	private JTextField lastName;
	private JTextField birthDate;
	private JTextField birthPlace;
	public static OwnerPanel instance;
	private JTextField ssn;
	private boolean isCreating;

	/**
	 * Create the panel.
	 */
	public OwnerPanel() {
		buildLayout();
		isCreating = true;
	}
	
	public OwnerPanel(Owner owner) {
		buildLayout();
		isCreating = false;
	}
	
	private void buildLayout() {
		JLabel lblOwner = new JLabel("OWNER");
		lblOwner.setHorizontalAlignment(SwingConstants.CENTER);
		lblOwner.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		JLabel lblNewLabel = new JLabel("Name:");
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel lblLastname = new JLabel("LastName:");
		
		lastName = new JTextField();
		lastName.setColumns(10);
		
		JLabel lblBirthDate = new JLabel("Birth date:");
		
		birthDate = new JTextField();
		birthDate.setColumns(10);
		
		JLabel lblBirthPlace = new JLabel("Birth place:");
		
		birthPlace = new JTextField();
		birthPlace.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
		        Owner owner = new Owner();
		       		    
		        owner.setName(name.getText());
		        owner.setLastname(lastName.getText());
		        owner.setSSN(ssn.getText());
		       // owner.setPets(getListPets(petsList.getModel()));
		        owner.setBirthdate(new Date());
		        owner.setBirthplace(birthPlace.getText());
		        
		        
				DaoOwner daoOwner = new DaoOwner();
				
				if(isCreating){
					daoOwner.insertEntity(owner);
				}else{
					daoOwner.updateEntity(owner);
				}
		        
		        System.out.println();
							
		        clearTextbox();
				
				
			}
		});
		
		JLabel lblSsn = new JLabel("SSN:");
		
		ssn = new JTextField();
		ssn.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(lblOwner, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(121)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblBirthDate, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblBirthPlace, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE))
							.addGap(5)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(76)
									.addComponent(btnSave, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
								.addComponent(birthDate)
								.addComponent(birthPlace)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblLastname, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblSsn))
							.addGap(5)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(name, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
								.addComponent(ssn, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
								.addComponent(lastName, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))))
					.addGap(117))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(lblOwner, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addGap(19)
							.addComponent(lblLastname))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(13)
							.addComponent(lastName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblSsn)
						.addComponent(ssn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(lblBirthDate)
							.addGap(18)
							.addComponent(lblBirthPlace))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(birthDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(12)
							.addComponent(birthPlace, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(24)
					.addComponent(btnSave)
					.addContainerGap())
		);
		setLayout(groupLayout);
	}

	
	void clearTextbox(){
		
		name.setText(""); 
		lastName.setText("");
		ssn.setText("");
		birthDate.setText("");
		birthPlace.setText("");					
						
	}

	
	public void setTextFields(String n, String last, String s, String date, String place){
		
		name.setText(n);
		lastName.setText(last);
		ssn.setText(s);
		birthDate.setText(date);
		birthPlace.setText(place);			
		
	}
		

		
	}	
		
		
		
		
	

